import qs from 'qs';
import {ResponseType} from 'types';
import {del, get, post} from 'utils/request';

export default {
  /**
   * 校验用户是否登录并获取用户token
   * @param username 用户名
   * @param password 密码
   * @returns `{ok, data, msg}`
   */
  getToken: async (
    username: string,
    password: string,
  ): Promise<ResponseType> => {
    const res: ResponseType = await post('/auth/token', {
      username,
      password,
    });
    return res;
  },
  /**
   * 获取eventlist
   * @param offset 偏移量
   * @param limit 分页大小
   * @param channels 查询条件
   * @returns `{ok, data, msg}`
   */
  getEventList: async (
    offset: number,
    limit: number,
    channels: string,
    before?: number | string,
    after?: number | string,
  ): Promise<ResponseType> => {
    const res: ResponseType = await get(
      `/events?${qs.stringify({
        offset,
        limit,
        before,
        after,
        channels: channels,
      })}`,
    );
    return res;
  },
  getEventDetail: async (eventId: number): Promise<ResponseType> => {
    const res: ResponseType = await get(`/events/${eventId}`);
    return res;
  },
  /**
   * 加入event
   * @param eventId eventId
   * @returns `{ok, data, msg}`
   */
  joinEvent: async (eventId: number): Promise<ResponseType> => {
    const res: ResponseType = await post(`/events/${eventId}/participants`, {});
    return res;
  },
  delJoinEvent: async (eventId: number): Promise<ResponseType> => {
    const res: ResponseType = await del(`/events/${eventId}/participants`, {});
    return res;
  },
  /**
   * 关注event
   * @param eventId eventId
   * @returns `{ok, data, msg}`
   */
  likeEvent: async (eventId: number): Promise<ResponseType> => {
    const res: ResponseType = await post(`/events/${eventId}/likes`, {});
    return res;
  },
  getEventLike: async (eventId: number): Promise<ResponseType> => {
    const res: ResponseType = await await get(`/events/${eventId}/likes`);
    return res;
  },
  getEventPartic: async (eventId: number): Promise<ResponseType> => {
    const res: ResponseType = await await get(
      `/events/${eventId}/participants`,
    );
    return res;
  },
  dellikeEvent: async (eventId: number): Promise<ResponseType> => {
    const res: ResponseType = await del(`/events/${eventId}/likes`, {});
    return res;
  },
  getUserLike: async (offset: number, limit: number): Promise<ResponseType> => {
    const res: ResponseType = await get(
      `/user/events?${qs.stringify({
        offset,
        limit,
        type: 'liked',
      })}`,
    );
    return res;
  },
  getUserGoing: async (
    offset: number,
    limit: number,
  ): Promise<ResponseType> => {
    const res: ResponseType = await get(
      `/user/events?${qs.stringify({
        offset,
        limit,
        type: 'going',
      })}`,
    );
    return res;
  },
  getUserPast: async (offset: number, limit: number): Promise<ResponseType> => {
    const res: ResponseType = await get(
      `/user/events?${qs.stringify({
        offset,
        limit,
        type: 'past',
      })}`,
    );
    return res;
  },
  addComment: async (
    eventId: number,
    comment: string,
  ): Promise<ResponseType> => {
    const res: ResponseType = await post(`/events/${eventId}/comments`, {
      comment,
    });
    return res;
  },
};
